from sqlmodel import Session, SQLModel, create_engine
from fastapi import APIRouter
from model.user_model import User
from model.chat_model import ChatDB, ChatMessageDB

sqlite = APIRouter()

#数据库配置
sqlite_file_name = "TokAI.db"
sqlite_url = f"sqlite:///{sqlite_file_name}"
#创建数据库
connect_args = {"check_same_thread": False}
engine = create_engine(sqlite_url, connect_args=connect_args)

#创建启动表
def create_db_and_tables():
    # 确保User表先创建
    User.metadata.create_all(engine)
    # 然后创建其他表
    ChatDB.metadata.create_all(engine)
    ChatMessageDB.metadata.create_all(engine)

#创建数据库会话
def get_session():#生成器创建数据库会话
    with Session(engine) as session:
        yield session
